<template>
  <div class="page-home">
    <div class="home-entry">
      <div class="container">
        <!-- 左侧分类 -->
        <HomeCategory></HomeCategory>
        <!-- banner轮播图 -->
        <!-- 可以大驼峰写法，也可以 xx-xx 的写法  -->
        <!-- <HomeBanner></HomeBanner> -->
        <home-banner></home-banner>

        <HomePanel
          ref="newTarget"
          title="新鲜好物"
          subTitle="新鲜出炉 品质靠谱"
        >
          <template #right>
            <XtxMore></XtxMore>
          </template>
          <ul class="new-list" v-if="home.newList.length > 0">
            <li v-for="item in home.newList" :key="item.id">
              <RouterLink :to="`/goods/${item.id}`">
                <img :src="item.picture" alt="" />
                <p>{{ item.name }}</p>
                <p class="price">{{ item.price }}</p>
              </RouterLink>
            </li>
          </ul>
          <div class="home-skeleton" v-else>
            <div
              class="item"
              v-for="i in 4"
              :key="i"
              :style="{ backgroundColor: '#f0f9f4' }"
            >
              <XtxSkeleton bg="#e4e4e4" :width="306" :height="306" />
              <XtxSkeleton bg="#e4e4e4" :width="160" :height="24" />
              <XtxSkeleton bg="#e4e4e4" :width="120" :height="24" />
            </div>
          </div>
        </HomePanel>
        <!-- 面板组件 -->
        <HomePanel
          ref="hotTarget"
          title="人气推荐"
          subTitle="人气爆款 不容错过"
        >
          <ul class="hot-list" v-if="home.hotList.length > 0">
            <li v-for="item in home.hotList" :key="item.id">
              <RouterLink to="/">
                <img :src="item.picture" alt="" />
                <p>{{ item.title }}</p>
                <p class="desc">{{ item.alt }}</p>
              </RouterLink>
            </li>
          </ul>
          <div class="home-skeleton" v-else>
            <div
              class="item"
              v-for="i in 4"
              :key="i"
              :style="{ backgroundColor: '#f0f9f4' }"
            >
              <XtxSkeleton bg="#e4e4e4" :width="306" :height="306" />
              <XtxSkeleton bg="#e4e4e4" :width="160" :height="24" />
              <XtxSkeleton bg="#e4e4e4" :width="120" :height="24" />
            </div>
          </div>
        </HomePanel>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import HomeCategory from "./components/home-category.vue";
import HomeBanner from "./components/home-banner.vue";
import HomePanel from "./components/home-panel.vue";
import useStore from "@/store";
// 未封装的代码 ---引入
// import { useIntersectionObserver } from '@vueuse/core'
import { useLazyData } from "@/hook";

const { home } = useStore();

// 未封装的代码 ---开始
// const newTarget = ref(null)
// const hotTarget = ref(null)

// 数据懒加载之获取新鲜好物数据

// const { stop } = useIntersectionObserver(newTarget, ([{ isIntersecting }]) => {
//   if (isIntersecting) {
//     stop()
//     home.getNewList()
//   }
// })

// const { stop: stop2 } = useIntersectionObserver(
//   hotTarget,
//   ([{ isIntersecting }]) => {
//     if (isIntersecting) {
//       stop2()
//       home.getHotList()
//     }
//   }
// )

// 未封装的代码 ---结束

const { target: newTarget } = useLazyData(() => {
  home.getNewList();
});

const { target: hotTarget } = useLazyData(() => {
  home.getHotList();
});

// home.getNewList()
// home.getHotList()
</script>

<style scoped lang="less">
.new-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
      display: inline-block;
      white-space: nowrap;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .price {
      color: @priceColor;
    }
  }
}
.hot-list {
  display: flex;
  justify-content: space-between;
  height: 426px;
  li {
    width: 306px;
    height: 406px;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
.home-skeleton {
  width: 1240px;
  height: 406px;
  display: flex;
  justify-content: space-between;
  .item {
    width: 306px;
    .xtx-skeleton ~ .xtx-skeleton {
      display: block;
      margin: 16px auto 0;
    }
  }
}
</style>
